<?php

namespace app\admin\controller;

use app\admin\service\CommonService;
use app\admin\service\CustomerService;
use app\admin\validate\StudentValidate;
use app\models\DictionaryModel;
use app\models\FollowUpLogModel;
use app\models\StudentModel;
use app\models\UserModel;


/**
 * Class Customer.
 */
class Customer extends Common
{
	/**
	 * StudentModel 学员模型.
	 *
	 * @var StudentModel
	 */
	protected $studentModel;

	/**
	 * 用户模型.
	 *
	 * @var UserModel
	 */
	protected $userModel;

	/**
	 * 跟进沟通记录模型.
	 *
	 * @var FollowUpLogModel
	 */
	protected $followUpLogModel;

	/**
	 * 验证对象.
	 *
	 * @var StudentValidate
	 */
	protected $studentValidate;

	/**
	 * 我的客户.
	 *
	 * @throws \think\Exception
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 *
	 * @return \think\response\Json|\think\response\View
	 */
	public function lists()
	{
		if ($this->request->isAjax()) {
			$param = $this->request->param();
			$param['is_audition'] = ['in','0,1'];
			if(get_current_type() == 5){  //员工
				$param['status'] = 1;
				$param['user_id'] = get_current_id();
			}
			$param['source_status'] = 2;  //表示客户
			$course = $this->studentModel->getStudentLists($param);

			$this->success('success', '', $course, 0);
		}

		//先定义一个数组
		$weekarray = ['日', '一', '二', '三', '四', '五', '六'];
		$where_user = [];
		$where_user['status'] = 1;
		$where_user['id'] = ['neq',get_current_id()];
		$users = $this->userModel->where($where_user)->field('id,name')->select()->toArray();
		$arr[] = [
			'id' => 0,
			'name' => '公海',
		];
		$users = array_merge($users, $arr);
		$this->dictionary();
		//今日新增客户
		$today_add_customer = (new StudentModel())->where(['register_date'=>date('Y-m-d')])->count();
		//今日跟进客户
		$today_follow_up_customer = (new StudentModel())->where(['follow_up_date'=>date('Y-m-d')])->count();
		//今日签单
		$today_signing = (new StudentModel())->where(['intentional_grade'=>1])->whereTime('follow_up_time', 'today')->count();
		//本月签单
		$month_signing = (new StudentModel())->where(['intentional_grade'=>1])->whereTime('follow_up_time', 'month')->count();
		//今日重点客户
		$today_key_customers = (new StudentModel())->where(['intentional_grade'=>3])->whereTime('follow_up_time', 'today')->count();
		//本月重点客户
		$month_key_customers = (new StudentModel())->where(['intentional_grade'=>3])->whereTime('follow_up_time', 'month')->count();
		//所有重点客户
		$all_key_customers = (new StudentModel())->where(['intentional_grade'=>3])->count();

//		//总签单排名
//		$user_rank = (new StudentModel())->where(['intentional_grade'=>1,'user_id'=>get_current_id()])->count();
//		//本月签单排名
//		$user_month_rank = (new StudentModel())->where(['intentional_grade'=>1,'user_id'=>get_current_id()])->whereTime('follow_up_time', 'month')->count();
		$this->assign('today_add_customer', $today_add_customer);
		$this->assign('today_follow_up_customer', $today_follow_up_customer);
		$this->assign('today_signing', $today_signing);
		$this->assign('month_signing', $month_signing);
		$this->assign('today_key_customers', $today_key_customers);
		$this->assign('month_key_customers', $month_key_customers);
		$this->assign('all_key_customers', $all_key_customers);

		$this->assign('users', $users);
		$this->assign('time', date('Y-m-d'));
		$this->assign('week', $weekarray[date('w')]);
		if (get_current_type() == 4 || get_current_type() == 5) {
			$type = 1;
		} else {
			$type = 0;
		}
		$Type_Student = get_current_type();
		return $this->fetch('', [
			'type' => $type,
			'Type_Student' => $Type_Student
		]);
	}

	/**
	 * 公海客户
	 * User: Feng
	 * Date: 2020/09/04
	 *
	 * @return mixed
	 * @throws \think\Exception
	 * @throws \think\exception\DbException
	 */
	public function public_index()
	{
		if ($this->request->isAjax()) {
			$param = $this->request->param();
			$param['is_audition'] = ['in','0,1'];
			if(get_current_type() == 5){  //员工
				$param['status'] = 1;
				$param['user_id'] = get_current_id();
			}
			$param['source_status'] = 2;  //表示客户
			$param['type'] = 'public';
			$course = $this->studentModel->getStudentLists($param);

			$this->success('success', '', $course, 0);
		}
		$where_user = [];
		$where_user['status'] = 1;
		$where_user['id'] = ['neq',get_current_id()];
		$users = $this->userModel->where($where_user)->field('id,name')->select()->toArray();
		$arr[] = [
			'id' => 0,
			'name' => '公海',
		];
		$users = array_merge($users, $arr);
		$Type_Student = get_current_type();
		return $this->fetch('', [
			'Type_Student' => $Type_Student,
			'users' => $users,
		]);
	}

	/**
	 * 无效客户
	 * User: Feng
	 * Date: 2020/09/04
	 *
	 * @return mixed
	 * @throws \think\Exception
	 * @throws \think\exception\DbException
	 */
	public function invalid_index()
	{
		if ($this->request->isAjax()) {
			$param = $this->request->param();
			$param['is_audition'] = ['in','0,1'];
			if(get_current_type() == 5){  //员工
				$param['status'] = 1;
				$param['user_id'] = get_current_id();
			}
			$param['source_status'] = 2;  //表示客户
			$param['type'] = 'invalid';
			$course = $this->studentModel->getStudentLists($param);

			$this->success('success', '', $course, 0);
		}
		$where_user = [];
		$where_user['status'] = 1;
		$where_user['id'] = ['neq',get_current_id()];
		$users = $this->userModel->where($where_user)->field('id,name')->select()->toArray();
		$arr[] = [
			'id' => 0,
			'name' => '公海',
		];
		$users = array_merge($users, $arr);
		$Type_Student = get_current_type();
		return $this->fetch('', [
			'Type_Student' => $Type_Student,
			'users' => $users,
		]);
	}

	/**
	 * 已分配客户
	 * User: Feng
	 * Date: 2020/09/04
	 *
	 * @return mixed
	 * @throws \think\Exception
	 * @throws \think\exception\DbException
	 */
	public function assigned_index()
	{
		if ($this->request->isAjax()) {
			$param = $this->request->param();
			$param['is_audition'] = ['in','0,1'];
			if(get_current_type() == 5){  //员工
				$param['status'] = 1;
				$param['user_id'] = get_current_id();
			}
			$param['source_status'] = 2;  //表示客户
			$param['type'] = 'assigned';
			$course = $this->studentModel->getStudentLists($param);

			$this->success('success', '', $course, 0);
		}
		$where_user = [];
		$where_user['status'] = 1;
		$where_user['id'] = ['neq',get_current_id()];
		$users = $this->userModel->where($where_user)->field('id,name')->select()->toArray();
		$arr[] = [
			'id' => 0,
			'name' => '公海',
		];
		$users = array_merge($users, $arr);
		$Type_Student = get_current_type();
		return $this->fetch('', [
			'Type_Student' => $Type_Student,
			'users' => $users,
		]);
	}

	/**
	 * 新客户新增.
	 *
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 *
	 * @return \think\response\Json|\think\response\View
	 */
	public function add()
	{
		if ($this->request->isAjax() && $this->request->isPost()) {
			$data = input('post.');
			if($data['user_id'] == 0){
				$data['course_consultant_type']  = 2; //公海
				$data['course_consultant'] = '公海';
				$data['user_id'] = 0;
			}else{
				$data['course_consultant_type']  = 1; //公海
				$data['user_id'] = $data['user_id'];
				$data['course_consultant'] = $this->userModel->get(['id' => $data['user_id']])['name'];
			}
			$check = (new StudentModel())->get(['phone'=>$data['phone']]);
			if($check){
				$this->error('客户已存在', null, '', 1);
			}
			$data['is_audition'] = 1;
			$data['student_name'] = $data['name'];
			$data['student_number'] = CommonService::generateStudentNumber();  //学号
			$data['register_date'] = date('Y-m-d');
			$student = $this->studentModel->save($data);
			$id = $this->studentModel->id;
			$data = [
				'student_id' => $id, //学员客户ID
				'user_id' => get_current_id(),
				'timestamp' => time(),//时间戳
				'date' => date('Y-m-d', time()), //日期
				'time' => date('Y-m-d H:i:s', time()), //时间
				'type' => 1,
				'remark' => get_current_name().'添加了客户'.'<span style="color: #ff224b;">'.$student['student_name'].'</span>',
			];
			FollowUpLogModel::create($data);
			if (false !== $student) {
				$this->success('保存成功', null, '', 1);
			}
			$this->error('保存失败', null, '', 1);
		}
		$this->dictionary();
		$where = [];
		$where['status'] = 1;
		$students = $this->studentModel->where($where)->select()->toArray();
		$current_type = get_current_type();

		$where_user = [];
		$where_user['status'] = 1;
		if(get_current_type() == 5){
			$where_user['id'] = get_current_id();
		}
		$users = $this->userModel->where($where_user)->field('id,name')->select()->toArray();
		if(get_current_type() != 5){
			$arr[] = [
				'id' => 0,
				'name' => '公海',
			];
			$users = array_merge($users, $arr);
		}

		return $this->fetch('', [
			'students' => $students,
			'users' => $users,
			'current_type' => $current_type,
		]);
	}

	/**
	 * 编辑客户.
	 *
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 *
	 * @return \think\response\Json|\think\response\View
	 */
	public function edit()
	{
		if ($this->request->isAjax() && $this->request->isPost()) {
			$data = input();
			StudentModel::update($data);
			return ['code' => 0, 'msg' => '保存成功！ 页面即将自动刷新~'];
		}
	}

	/**
	 * 客户信息.
	 *
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 *
	 * @return mixed|\think\response\Json
	 */
	public function information()
	{
		if (request()->isPost()) {
			return json((new CustomerService())->follow_up());
		}
		$student = StudentModel::get(input('id'));
		$status = input('status');
		if (0 === $student['intentional_grade']) {
			$student['intentional_grade_text'] = '0类';
		} elseif (1 === $student['intentional_grade']) {
			$student['intentional_grade_text'] = '1类';
		} elseif (2 === $student['intentional_grade']) {
			$student['intentional_grade_text'] = '2类';
		} elseif (3 === $student['intentional_grade']) {
			$student['intentional_grade_text'] = '3类';
		} elseif (4 === $student['intentional_grade']) {
			$student['intentional_grade_text'] = '4类';
		} elseif (5 === $student['intentional_grade']) {
			$student['intentional_grade_text'] = '5类';
		}

		$student['follow_up_text'] = DictionaryModel::getById($student['follow_up'], 'name');
		if ($student['channel'] > 0) {
			$student['channel'] = DictionaryModel::getById($student['channel'], 'name');
		} elseif (-1 === $student['channel']) {
			$student['channel'] = '转介绍';
		} else {
			$student['channel'] = '--';
		}
		$followUpLog = $this->followUpLogModel->where(['student_id' => input('id')])->order('timestamp desc')->find();
		$student['date'] = $followUpLog['date'];
		$student['time'] = $followUpLog['time'];
		$logs = $this->followUpLogModel
			->where(['student_id' => input('id')])
			->order('timestamp desc')
			->select()
			->toArray();
		foreach ($logs as &$log) {
			if (1 === $log['type']) {
				$log['type_text'] = '系统';
				$log['avatar'] = '/favicon.ico';
			} else {
				$log['type_text'] = get_current_name();
				$log['avatar'] = get_current_avatar() ? get_current_avatar() : '/favicon.ico';
			}

			if (1 === $log['is_kp']) {
				$log['is_kp_text'] = '否';
			} elseif (2 === $log['is_kp']) {
				$log['is_kp_text'] = '是';
			}
			if (1 === $log['is_contact']) {
				$log['is_contact_text'] = '否';
			} elseif (2 === $log['is_contact']) {
				$log['is_contact_text'] = '是';
			}

			if (0 === $log['intentional_grade']) {
				$log['intentional_grade_text'] = '0类';
			} elseif (1 === $log['intentional_grade']) {
				$log['intentional_grade_text'] = '1类';
			} elseif (2 === $log['intentional_grade']) {
				$log['intentional_grade_text'] = '2类';
			} elseif (3 === $log['intentional_grade']) {
				$log['intentional_grade_text'] = '3类';
			} elseif (4 === $log['intentional_grade']) {
				$log['intentional_grade_text'] = '4类';
			} elseif (5 === $log['intentional_grade']) {
				$log['intentional_grade_text'] = '5类';
			}
		}

		$this->dictionary();
		$Type_Student = get_current_type();
		return $this->fetch('', [
			'user' => $this->userModel->get(get_current_id()),
			'Type_Student' => $Type_Student,
			'status' => $status,
			'student' => $student,
			'logs' => $logs,
			'date' => date('Y-m-d')
		]);
	}

	/**
	 * 添加跟进记录.
	 *
	 * @throws \think\exception\DbException
	 */
	public function addFollowUp()
	{
		if ($this->request->isAjax() && $this->request->isPost()) {
			$data = input('post.');
			$data['timestamp'] = time(); // 跟进记录时间戳
			$data['time'] = date('Y-m-d H:i:s'); // 跟进记录时间
			$data['date'] = date('Y-m-d'); // 跟进记录日期
			$data['user_id'] = get_current_id(); // 操作人ID
			$data['img'] = get_current_avatar(); // 操作人头像
			$data['type'] = 2; // 沟通记录
			if(isset($data['is_contact']) && $data['is_contact'] == 'on'){
				$data['is_contact'] = 2;
			}else{
				$data['is_contact'] = 1;
			}
			if(isset($data['is_kp'])){
				$data['is_kp'] = $data['is_kp'];
			}else{
				$data['is_kp'] = 1;
			}
			$followUpLog = $this->followUpLogModel->save($data);
			$data['id'] = $this->followUpLogModel->id;
			$_data = array(
				'intentional_grade'=>$data['intentional_grade'],//意向级别
				'follow_up_date'=>date('Y-m-d'),//跟进日期
				'follow_up_time'=>date('Y-m-d H:i:s'),//跟进时间
				'follow_up_timestamp'=>time(),//跟进时间戳
				'next_follow_up_date'=>date('Y-m-d',strtotime($data['next_follow_up_time'])),//下次跟进日期
				'next_follow_up_time'=>$data['next_follow_up_time'],//下次跟进时间
				'next_follow_up_timestamp'=>strtotime($data['next_follow_up_time']),//跟进时间戳
			);

			$this->studentModel->save($_data,['id'=>$data['student_id']]);
			$data['type_text'] = get_current_name();
			$data['avatar'] = get_current_avatar();

			if (0 === $data['intentional_grade']) {
				$data['intentional_grade_text'] = '0类';
			} elseif (1 === $data['intentional_grade']) {
				$data['intentional_grade_text'] = '1类';
			} elseif (2 === $data['intentional_grade']) {
				$data['intentional_grade_text'] = '2类';
			} elseif (3 === $data['intentional_grade']) {
				$data['intentional_grade_text'] = '3类';
			} elseif (4 === $data['intentional_grade']) {
				$data['intentional_grade_text'] = '4类';
			} elseif (5 === $data['intentional_grade']) {
				$data['intentional_grade_text'] = '5类';
			}

			if (false !== $followUpLog) {
				$this->success('添加成功', '', $data, 1);
			}
			$this->error('添加失败', null, '', 1);
		}
	}

	/**
	 * 领取客户.
	 *
	 * @throws \think\exception\DbException
	 */
	public function receive_customer()
	{
		if ($this->request->isAjax() && $this->request->isPost()) {
			$where['id'] = ['in', input('ids')];
			$arr = explode(',', input('ids'));
			$res = $this->studentModel->save(['course_consultant_type' => 1, 'course_consultant' => get_current_name(), 'user_id' => get_current_id()], $where);
			foreach ($arr as $id) {
				// 学员客户跟进沟通记录写入
				$student = StudentModel::get($id);
				$data = [
					'student_id' => $id, //学员客户ID
					'user_id' => get_current_id(),
					'timestamp' => time(),//时间戳
					'date' => date('Y-m-d', time()), //日期
					'time' => date('Y-m-d H:i:s', time()), //时间
					'type' => 1,
					'remark' => get_current_name().'领取了'.'<span style="color: #ff224b;">'.$student['student_name'].'</span>'.'这个客户',
				];
				FollowUpLogModel::create($data);
			}
			if (false === $res) {
				$this->error('领取失败', null, '', 2);
			}
			$this->success('领取成功', null, '', 1);
		}
	}

	/**
	 * 无效转为有效客户
	 * 选择转为系统顾问还是公海.
	 *
	 * @throws \think\exception\DbException
	 *
	 * @return mixed
	 */
	public function effective()
	{
		if ($this->request->isAjax() && $this->request->isPost()) {
			if ('1' === input('course_consultant_type')) {
				$name = get_current_name();
				$user_id = get_current_id();
				$follow_up = 34;//未处理
			} else {
				$name = '公海';
				$user_id = 0;
				$follow_up = 33;//未分配
			}
			$where['id'] = ['in', input('ids')];

			$arr = explode(',', input('ids'));
			$res = $this->studentModel->save(['status' => 1, 'follow_up'=>$follow_up,'course_consultant_type' => input('course_consultant_type'), 'course_consultant' => $name, 'user_id' => $user_id], $where);
			foreach ($arr as $id) {
				// 学员客户跟进沟通记录写入
				$student = StudentModel::get($id);
				$data = [
					'student_id' => $id, //学员客户ID
					'user_id' => get_current_id(),
					'timestamp' => time(),//时间戳
					'date' => date('Y-m-d', time()), //日期
					'time' => date('Y-m-d H:i:s', time()), //时间
					'type' => 1,
					'remark' => get_current_name().'将无效客户'.'<span style="color: #ff224b;">'.$student['student_name'].'</span>'.'转为归属'.' '.$name.' '.'的有效客户',
				];
				FollowUpLogModel::create($data);
			}
			if (false === $res) {
				$this->error('操作失败', null, '', 2);
			}
			$this->success('操作成功', null, '', 1);
		}

		return $this->fetch('', [
			'ids' => input('ids')
		]);
	}

	/**
	 * 将客户转给同事.
	 *
	 * @throws \think\exception\DbException
	 *
	 * @return mixed
	 */
	public function assignment()
	{
		if ($this->request->isAjax() && $this->request->isPost()) {
			$data = input('post.');
			$where['id'] = ['in', input('ids')];
			$arr = explode(',', input('ids'));
			$data['name'] = $this->userModel->get(['id' => $data['user_id']])['name'];
			$res = $this->studentModel->save(['course_consultant_type' => 1, 'switch_to'=>1, 'intentional_grade'=>0, 'user_id' => $data['user_id'], 'course_consultant' => $data['name']], $where);
			foreach ($arr as $id) {
				// 学员客户跟进沟通记录写入
				$student = StudentModel::get($id);
				$datas = [
					'student_id' => $id, //学员客户ID
					'user_id' => get_current_id(),
					'timestamp' => time(),//时间戳
					'date' => date('Y-m-d', time()), //日期
					'time' => date('Y-m-d H:i:s', time()), //时间
					'type' => 1,
					'remark' => get_current_name().'将客户'.'<span style="color: #ff224b;">'.$student['student_name'].'</span>'.'转给了同事'.$data['name'],
				];
				FollowUpLogModel::create($datas);
			}
			if (false === $res) {
				$this->error('操作失败', null, '', 2);
			}
			$this->success('操作成功', null, '', 1);
		}

		return $this->fetch('', [
			'ids' => input('ids'),
			'status' => input('status')
		]);
	}

	/**
	 * 当前校区的所有用户.
	 *
	 * @throws \think\exception\DbException
	 */
	public function query()
	{
		if ($this->request->isAjax()) {
			$param = $this->request->param();
			$list = $this->userModel->getLists($param);

			$this->success('success', '', $list, 0);
		}
	}

	/**
	 * 释放客户到公海.
	 *
	 * @throws \think\exception\DbException
	 *
	 * @return mixed
	 */
	public function release()
	{
		if ($this->request->isAjax() && $this->request->isPost()) {
			$_data = $where = [];
			$where['id'] = ['in', input('ids')];
			$arr = explode(',', input('ids'));
			$_data['next_follow_up_date'] = null;
			$_data['next_follow_up_time'] = null;
			$_data['next_follow_up_timestamp'] = null;
			$_data['course_consultant_type'] = 2;
			$_data['intentional_grade'] = 0;
			$_data['course_consultant'] = '公海';
			$_data['user_id'] = 0;
			$_data['reason'] = input('reason');
			$res = $this->studentModel->save($_data, $where);
			foreach ($arr as $id) {
				// 学员客户跟进沟通记录写入
				$student = StudentModel::get($id);
				$data = [
					'student_id' => $id, //学员客户ID
					'user_id' => get_current_id(),
					'timestamp' => time(),//时间戳
					'date' => date('Y-m-d', time()), //日期
					'time' => date('Y-m-d H:i:s', time()), //时间
					'type' => 1,
					'remark' => get_current_name().'将客户'.'<span style="color: #ff224b;">'.$student['student_name'].'</span>'.'释放到了公海'.'<br/>'.'释放原因：'.input('reason'),
				];
				FollowUpLogModel::create($data);
			}
			if (false === $res) {
				$this->error('释放失败', null, '', 2);
			}
			$this->success('释放成功', null, '', 1);
		}

		return $this->fetch('', [
			'ids' => input('ids')
		]);
	}

	/**
	 * 将客户标记为无效.
	 *
	 * @throws \think\exception\DbException
	 *
	 * @return mixed
	 */
	public function invalid()
	{
		if ($this->request->isAjax() && $this->request->isPost()) {
			$where['id'] = ['in', input('ids')];

			$arr = explode(',', input('ids'));
			foreach ($arr as $id) {
				// 学员客户跟进沟通记录写入
				$student = StudentModel::get($id);
				if($student['is_audition'] == 2){
					$this->error($student['name'].'为正式学员,无法转为无效客户', null, '', 2);
				}
			}

			$res = $this->studentModel->save(['course_consultant_type' => 0, 'course_consultant' => '-', 'status' => 2, 'reason' => input('reason'), 'user_id' => 0], $where);

			foreach ($arr as $id) {
				// 学员客户跟进沟通记录写入
				$student = StudentModel::get($id);
				$data = [
					'student_id' => $id, //学员客户ID
					'user_id' => get_current_id(),
					'timestamp' => time(),//时间戳
					'date' => date('Y-m-d', time()), //日期
					'time' => date('Y-m-d H:i:s', time()), //时间
					'type' => 1,
					'remark' => get_current_name().'将客户'.'<span style="color: #ff224b;">'.$student['student_name'].'</span>'.'的状态修改为无效'.'<br/>'.'无效原因：'.input('reason'),
				];
				FollowUpLogModel::create($data);
			}
			if (false === $res) {
				$this->error('标记失败', null, '', 2);
			}
			$this->success('标记成功', null, '', 1);
		}

		return $this->fetch('', [
			'ids' => input('ids')
		]);
	}

	/**
	 * 移除客户.
	 */
	public function delete()
	{
		if ($this->request->isAjax() && $this->request->isPost()) {
			$where['id'] = ['in', input('ids')];
			$res = $this->studentModel->where($where)->delete();
			if (false === $res) {
				$this->error('移除失败', null, '', 2);
			}
			$this->success('移除成功', null, '', 1);
		}
	}

	//获取校区招生报表
	public function stock()
	{
		if ($this->request->isAjax()) {
			$param = $this->request->param();
			$result = $this->studentModel->getStockList($param);
			$this->success('success', '', $result, 0);
		}
		return $this->fetch();
	}

	/**
	 * 导入客户表.
	 *
	 * @return mixed
	 */
	public function customer_excel()
	{

		try {
			if (request()->isPost()){
				$file = request()->file('file');
				if($file->getInfo()['error'] == 0){
					$filename = $file->getInfo()['name'];

					$file_type = explode(".",$filename)[1];
					if(strcasecmp($file_type,"Xlsx")== 0 || strcasecmp($file_type,"Xls") == 0
						|| strcasecmp($file_type,"Xml") == 0 || strcasecmp($file_type,"Ods") == 0
						|| strcasecmp($file_type,"Slk") == 0 || strcasecmp($file_type,"Gnumeric") == 0
						|| strcasecmp($file_type,"Html") == 0 || strcasecmp($file_type,"Csv") == 0){

						$filename = '客户资料.'.$file_type;
						$file->move(ROOT_PATH . 'public',$filename); //上传后的文件名
						$excelData = readExcelFile(ROOT_PATH . 'public/'.$filename); //读表
						if ($excelData != null) {
							if(is_file(ROOT_PATH . 'public/'.$filename) && unlink(ROOT_PATH . 'public/'.$filename)){
								//插入数据库
								$student_table = []; //学员表

								foreach ($excelData as $key => $value){
									if(!$this->studentModel->get(['phone' => $value[1]])){
										$item['name'] = $value[0]; //书店名称
										$item['phone'] = $value[1]; //电话
										$item['address'] = $value[2];  //省市区
										$item['intentional_grade'] = 0; //客户成熟度
										$item['course_consultant'] = $value[3]; //跟进人
										$item['customer_name'] = $value[4]; //书店联系人
										$item['guardian_phone'] = $value[5]; //手机号码
										$item['other_phone'] = $value[6]; //更多联系人
										$item['weixin'] = $value[7];  //微信
										$item['qq'] = $value[8];  //QQ
										$item['remark'] = $value[9];  //备注
										$item['detailed_address'] = $value[10];  //详细地址
										$item['user_id'] = get_current_id(); //当前用户ID
										$item['course_consultant'] = get_current_name(); //当前用户名称
										$item['course_consultant_type'] = 1; //当前用户类型
										$item['is_audition'] = 1; //类型
										$item['register_date'] = date('Y-m-d');; //注册日期
										$item['follow_up'] = 34; //未处理
										$item['student_number'] =  CommonService::generateStudentNumber();  //学号
										$student_table[] = $item;
									}
								}

								if(count($student_table) > 0){
									$this->studentModel->saveAll($student_table);
									return ['code' => 0, 'data' => $excelData, 'name' => $filename, 'size'=>$file->getInfo()['size']];
								}else{
									return ['code' => 0, 'name' => $filename];
								}
							}

						}
					}
				}



			}

		} catch (\Exception $e) {
			return ['code' => 1, 'msg' => $e->getMessage()];
		}
	}

	//获取当月第一天和最后一天
	function getthemonth($date)
	{
		$firstday = date('Y-m-01', strtotime($date));
		$lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
		return array($firstday,$lastday);
	}

	public function outbound_call()
	{
		$data = input();
		return $this->fetch('',[
			'phone'=>$data['phone']
		]);
	}

	/**
	 * 实例化.
	 *
	 * @return bool|void
	 */
	protected function _initialize()
	{
		parent::_initialize(); // TODO: Change the autogenerated stub
		$this->studentModel = new StudentModel();
		$this->userModel = new UserModel();
		$this->followUpLogModel = new FollowUpLogModel();
		$this->studentValidate = new StudentValidate();
	}
}
